package com.example.lin.zyfypt_327ljd.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.lin.zyfypt_327ljd.R;
import com.example.lin.zyfypt_327ljd.activity.FocusActivity;
import com.example.lin.zyfypt_327ljd.bean.FocusBean;
import com.example.lin.zyfypt_327ljd.bean.UserBean;
import com.example.lin.zyfypt_327ljd.iface.FocusListener;
import com.example.lin.zyfypt_327ljd.model.FocusModel;
import com.squareup.picasso.Picasso;


import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by lin on 2017/4/13.
 */

public class FocusAdapter extends RecyclerView.Adapter {
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<FocusBean<UserBean>>  list;//保存要显示的数据
    private SharedPreferences sp;

    //1自定义：构造方法，传进上下文
    public FocusAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    //2自定义：获取数据
    public void setList(List<FocusBean<UserBean>> list)
    {
        this.list=list;
    }
    //3自定义：ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder
    {   private ImageView imageView;
        private TextView tvname;
        private Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tvname=(TextView)itemView.findViewById(R.id.textView);
            button=(Button)itemView.findViewById(R.id.button);
        }
    }
    //4重写：生成Item的View
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item4,parent,false);
        return new ViewHolder(v);
    }
    //5重写：给ViewHolder中的控件填充值，设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final FocusListener listener = new FocusListener() {
            @Override
            public void onResponse(String msg) {
                if (msg.equals("5")) {
                    list.remove(position);
                    notifyItemRemoved(position);
                    Toast.makeText(context, "取消关注成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "取消关注失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        };
        final UserBean userBean=list.get(position).getBean();
        if(userBean==null)
            return;
        final ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.tvname.setText(userBean.getRealname());
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"
                        +userBean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp = context.getSharedPreferences("login", MODE_PRIVATE);
                FocusModel model = new FocusModel();
                model.unfocus("userfocus", list.get(position).getIdolid(),sp.getString("sessionid", null), listener);
            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前条目信息，如id
//                int idolid = list.get(position).getIdolid();
//                String realname = list.get(position).getBean().getRealname();
//                //实际中，点击后打开新窗口做其他操作
//                Intent intent=new Intent(context,FocusActivity.class);
//                intent.putExtra("idolid",idolid);
//                intent.putExtra("realname", realname);
//                context.startActivity(intent);
            }
        });
    }
    //共有多少条目
    @Override
    public int getItemCount() {
        if(list==null)  return 0;
        else return list.size();
    }
}
